/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hextd.util;

/**
 *
 * @author hylke
 */
public class RunningAverage {

	private int count;
	private int pointer = 0;
	private int[] values;
	private int sum = 0;
	private float average = 0;

	public RunningAverage(int count, int startValues) {
		this.count = count;
		this.values = new int[count];
		this.reset(startValues);
	}

	public float put(int value) {
		pointer++;
		if (pointer >= count) {
			pointer = 0;
		}
		sum -= values[pointer];
		values[pointer] = value;
		sum += value;

		average = 1.0f * sum / count;
		return average;
	}

	public void reset(int value) {
		for (int i = 0; i < count; i++) {
			values[i] = value;
		}
		sum = count * value;
		average = value;
	}

	public float getAverage() {
		return average;
	}
}
